package com.bigfire.dtm.core.event.listener.settlement;

import lombok.Builder;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 秒结订单崩溃日志处理类
 * @author Damon S.
 * @version v1.0.1
 * @date 2019年12月04日 20:26
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class UserOpsEventListener {

//    private final RealtimeLogDataHandler hwLogDataHandler;
//    private final RealtimeRewardDataHandler hwRewardDataHandler;
//    private final RealtimeRewardTranslator translator;

    private static final String MAX_ORDER_ID = "maxOrderId:";
    private static final String ORDER_ID_MESSAGE = "no unsettled orders found";

    /*
    @SuppressWarnings("unused")
    @EventHandler
    @Transactional(rollbackFor = Exception.class)
    void on(NoUnsettledFoundEvent event) {
        String message = MAX_ORDER_ID + event.getMaxOrderId() + Constants.STR_COMMA +
                (StringUtils.isEmpty(event.getMessage())
                        ? ORDER_ID_MESSAGE
                        : event.getMessage());
        this.hwLogDataHandler.persistCrashLog(
                CrashLog.builder().message(message)
                        .timestamp(event.getTimestamp()).build()
        );
    }

    @SuppressWarnings("unused")
    @EventHandler
    @Transactional(rollbackFor = Exception.class)
    void on(BigfireBlockedEvent event) {
        CrashLog.CrashLogBuilder builder = CrashLog.builder();
        Optional.ofNullable(event.getPhase()).ifPresent(
                phase -> builder.phase(phase.getCode())
        );
        this.hwLogDataHandler.persistCrashLog(
                builder.timestamp(event.getTimestamp())
                        .message(event.getMessage())
                        .orderNo(event.getOrderNo()).build()
        );
    }

    @SuppressWarnings("unused")
    @EventHandler
    @Transactional(rollbackFor = Exception.class)
    void on(OrderSettledEvent event) {
        // 保存晋升数据
        if (!CollectionUtils.isEmpty(event.getPromotions())) {
            hwRewardDataHandler.persistPromotions(event.getPromotions());
        }
        // 保存拨付数据
        if (!CollectionUtils.isEmpty(event.getRewards())) {
            hwRewardDataHandler.persistRewards(event.getRewards());
        }
        // 保存归属数据
        if (!PerformanceAscription.EMPTY.equals(event.getAscription())) {
            hwRewardDataHandler.persistAscription(event.getAscription());
        }
        log.info("{}订单[orderNo:{}]的秒结结果数据保存完成", Constants.PREFIX_SETTLEMENT, event.getOrderNo());
    }

    @SuppressWarnings("unused")
    @EventHandler
    @Transactional(rollbackFor = Exception.class)
    void on(OrderReverseFiredEvent event) {
        List<CustomerReward> pointReversedRewards = translator.toReversedPointReward(
                hwRewardDataHandler.findPointByOrderNo(event.getOrderNo()), event.getReverseType()
        );
        List<CustomerReward> creditReversedRewards = translator.toReversedCreditReward(
                hwRewardDataHandler.findCreditByOrderNo(event.getOrderNo()), event.getReverseType()
        );
        creditReversedRewards.addAll(pointReversedRewards);
        // 保存逆向拨付数据
        hwRewardDataHandler.persistRewards(creditReversedRewards);
        // 更新逆向归属数据
        hwRewardDataHandler.reversePerformance(event.getOrderNo());
        log.info("{}订单[orderNo:{}]逆向秒结/归属数据处理完成", Constants.PREFIX_SETTLEMENT, event.getOrderNo());
    }
    */


    @Value
    @Builder
    public static class CrashLog {
        String orderNo;
        String message;
        Integer phase;
        LocalDateTime timestamp;
    }
}
